/**
 * Copyright (c) 2012 Nokia Corporation.
 * All rights reserved.
 *
 * For the applicable distribution terms see the license text file included in
 * the distribution.
 */

#ifndef FACEBOOKCONNECT_H
#define FACEBOOKCONNECT_H

#include <QVariantMap>
#include <QDeclarativeItem>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QTime>
#include <QSettings>

class FacebookConnect : public QDeclarativeItem
{
    Q_OBJECT

    Q_PROPERTY(QString appId READ appId WRITE setAppId NOTIFY appIdChanged)
    Q_PROPERTY(QString accessToken READ accessToken WRITE setAccessToken NOTIFY accessTokenChanged)
    Q_PROPERTY(QString requestId READ requestId)
    Q_PROPERTY(bool authorized READ isAuthorized NOTIFY authorizedChanged)
    Q_PROPERTY(QStringList permissions READ permissions WRITE setPermissions NOTIFY permissionsChanged)
    Q_PROPERTY(QString organization READ organization WRITE setOrganization)
    Q_PROPERTY(QString appName READ appName WRITE setAppName)
    Q_ENUMS(HTTPMethod)

public:

    enum HTTPMethod {
        HTTPPost = 0,
        HTTPGet,
        HTTPPut,
        HTTPDelete
    };

public:
    explicit FacebookConnect(QDeclarativeItem *parent = 0);
    explicit FacebookConnect(const QString &appId,
                             const QString &organisation,
                             const QString &appName,
                             QDeclarativeItem *parent = 0);
    virtual ~FacebookConnect();

public slots: // Property setters and getters
    QString appId() const;
    void setAppId(const QString &appId);
    QString accessToken() const;
    void setAccessToken(const QString &accessToken);
    void setExpirationTime(const QString &expirationTime);
    bool isAuthorized() const;
    QString requestId() const;

    // @see https://developers.facebook.com/docs/reference/api/permissions/
    QStringList permissions() const;
    void setPermissions(const QStringList &permissions);

    QString organization() const;
    void setOrganization(const QString &organization);
    QString appName() const;
    void setAppName(const QString &appName);

public slots:
    void request(const QString &requestId, const QString &graphPath);
    void request(const QString &requestId,
                 const QString &graphPath,
                 const QVariantMap &parameters);
    void request(const QString &requestId,
                 const QString &graphPath,
                 const HTTPMethod method,
                 const QVariantMap &parameters);
    void logout();

private:
    void retrieveAccessTokenFromSettings();

private slots: // QNetworkAccessManager handling functions.
    void finished(QNetworkReply *reply);

signals:
    void appIdChanged(const QString &appId);
    void accessTokenChanged(const QString &token);
    void authorizedChanged(bool authorized);
    void permissionsChanged(const QStringList &permissions);
    void requestCompleted(const QString &requestId, const QByteArray &result);
    void requestFailed(const QString &requestId, const QString &reason);
    void logoutCompleted();

private: // Data
    QString m_appId;
    QString m_organisationName;
    QString m_applicationName;
    QString m_accessToken;
    QNetworkAccessManager m_networkAccess;
    QString m_requestId;
    QStringList m_permissions;
};

QML_DECLARE_TYPE(FacebookConnect)

#endif // FACEBOOKCONNECT_H

